/*
 * insn.h
 *
 *  Created on: 2024年5月1日
 *      Author: 25126
 */

#ifndef APPLICATION_INSN_H_
#define APPLICATION_INSN_H_

#include <hbird_sdk_soc.h>
#define  LEN    8

////////////////////////////////////////////////////////////
// custom0:
// Supported format: only R type here
// Supported instr:
//  1. custom0 lwnj: load data(in cpu) to njbuf
//		lw.nj (rs2), (rs1)
//	2. custom0 swnj: store data(in njbuf) to cpu
//		sw.nj (rd), (rs1)
//  3. custom0 lbuf: load data(in memory) to row_buf
//     lbuf (rs1)
//     .insn r opcode, func3, func7, rd, rs1, rs2    
//  4. custom0 sbuf: store data(in row_buf) to memory
//     sbuf (rs1)
//     .insn r opcode, func3, func7, rd, rs1, rs2    

////////////////////////////////////////////////////////////

// custom lw.nj
__STATIC_FORCEINLINE void custom_lwnj(int acc_addr, int main_reg)
{
    // lw.nj rs2, (rs1)
	int zero = 0;
    asm volatile (
       ".insn r 0x0b, 3, 0, x0, %1, %2"
    	:"=r"(zero)
    	:"r"(acc_addr), "r"(main_reg)
     );
}

// custom sw.nj
__STATIC_FORCEINLINE int custom_swnj(int acc_addr)
{
    int main_reg;
    
    //  sw.nj rd, (rs1)
    asm volatile (
       ".insn r 0x0b, 6, 1, %0, %1, x0"
    	:"=r"(main_reg)
    	:"r"(acc_addr)
     );
    return main_reg;
}

// custom lbuf
__STATIC_FORCEINLINE void custom_lbuf(int addr)
{
    int zero = 0;

    asm volatile (
       ".insn r 0x0b, 2, 2, x0, %1, x0"
           :"=r"(zero)
           :"r"(addr)
     );
}

// custom sbuf
__STATIC_FORCEINLINE void custom_sbuf(int addr)
{
    int zero = 0;
    asm volatile (
       ".insn r 0x0b, 2, 3, x0, %1, x0"
           :"=r"(zero)
           :"r"(addr)
     );
}


#endif /* APPLICATION_INSN_H_ */
